﻿using Glorysoft.Auto.Xfw;
using Glorysoft.Auto.Xfw.Component;
using Glorysoft.Auto.Xfw.Contract;
using Glorysoft.Auto.Xfw.Entities;
using Glorysoft.EAP.Core;
using Glorysoft.EAP.Core.Entities;
using Glorysoft.EAP.Core.Enums;
using Glorysoft.EAP.RC.EAPClient;
using Glorysoft.SECSwell;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Serilog.Core;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Glorysoft.EAP.EC.SECS.RCM
{
    /// <summary>
    ///  RCM联机
    /// </summary>
    public class RCMService : AbstractService
    {
        protected readonly XfwContext Context = XfwContext.Current;
        protected readonly Logger CimLogger;
        protected readonly string ServerName = "EAP";
        protected readonly string conn = "1";
        protected readonly string disConn = "-1";
        EAPClientService ClientService = null;
        private DbService dbServer = null;

        public RCMService()
        {
            try
            {
                CimLogger = XfwContext.LogManager.EapLogger;
                ClientService = new EAPClientService();
                dbServer = new DbService();
                //if (dbServer == null)
                //{
                //    dbServer = ReflectionHelper.Service<IDbService>();
                //}
            }
            catch (Exception ex)
            {
                CimLogger.Error(ex, $"{Name}.RCMService() Execute Error.");
            }
        }
        public virtual string Name => "RCMService";
        public string CurrentMethod => MethodBase.GetCurrentMethod().Name;

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="args"></param>
        public void EQP_Initialize(XfwArgs args)
        {
            try
            {
                var eqpID = args.SECSData.Context.Name;
                args.Header[CItems.EqpID] = eqpID;
                args.Header[CItems.EqpStatus] = conn;
                Dict.Current.RCMName = eqpID;
                Dict.Current.RCMConnStatus = conn;
                Console.WriteLine($"{eqpID}初始化");
                CimLogger.Information($"{Name}.EQP_Initialize() Execute Success.");
                args.SetReturnCodeOK();
                if (!Dict.Current.OCRCurrentStatus.ContainsKey(Dict.Current.TesterID))
                {
                    Dict.Current.OCRCurrentStatus.Add(Dict.Current.TesterID, "");
                }
                dbServer.InsertMsgInfo("RCM连接成功", eqpID, Dict.Current.lotInfo.lotId);

            }
            catch (Exception ex)
            {
                CimLogger.Error(ex, $"{Name}.EQP_Initialize() Execute Error.");
                args.SetReturnCodeNG($"EQP_Initialize:{ex.Message}");
            }
        }
        /// <summary>
        /// 断开连接
        /// </summary>
        /// <param name="args"></param>
        public void HsmsDisConnected(XfwArgs args)
        {
            try
            {
                var eqpID = args.SECSData.Context.Name;
                args.Header[CItems.EqpID] = eqpID;
                args.Header[CItems.EventID] = "47";
                args.Header[CItems.EqpStatus] = disConn;
                Dict.Current.RCMConnStatus = disConn;
                Dict.Current.RCMName = string.Empty;
                if (Dict.Current.OCRCurrentStatus.ContainsKey(Dict.Current.TesterID))
                {
                    Dict.Current.OCRCurrentStatus[Dict.Current.TesterID] = "";
                }
                else
                {
                    Dict.Current.OCRCurrentStatus.Add(eqpID, "");
                }
                CimLogger.Information($"{Name}.HsmsDisConnected() Execute Success.");
                dbServer.InsertMsgInfo("RCM已断开连接", eqpID, Dict.Current.lotInfo.lotId);

            }
            catch (Exception e)
            {
                CimLogger.Error(e, $"{Name}.HsmsDisConnected() Execute Error.");
            }
        }

        #region 设备主动发送 Request

        /// <summary>
        /// 发送设备是否可以通讯
        /// </summary>
        /// <param name="args"></param>
        public void Send_S1F1_AreYouThere(XfwArgs args)
        {
            try
            {
                var eqpID = args.Header[CItems.EqpID].ToString();
                var context = XfwContext.GetSECSContext(eqpID);
                var trans = context.GetTransaction(1, 1);
                var requestMsg = trans.Primary;
                var reply = context.SendAsync(trans);
                var replyMsg = reply.Result;
                if (replyMsg == null)
                {
                    args.SetReturnCodeNG("Send S1F1, Timeout");
                    CimLogger.Warning($"{Name}.Send_S1F1_AreYouThere() Timeout");
                    return;
                }
                Dict.Current.RCMConnStatus = conn;
                Dict.Current.RCMName = eqpID;
                if (!Dict.Current.OCRCurrentStatus.ContainsKey(Dict.Current.TesterID))
                {
                    Dict.Current.OCRCurrentStatus.Add(Dict.Current.TesterID, "");
                }
                args.SetReturnCodeOK();
                CimLogger.Information($"{Name}.Send_S1F1_AreYouThere() Execute Success.");
            }
            catch (Exception ex)
            {
                args.SetReturnCodeNG("S1F1:" + ex.Message);
                CimLogger.Error(ex, $"{Name}.Send_S1F1_AreYouThere() Execute Error.");
            }
        }
        /// <summary>
        /// 建立通讯
        /// </summary>
        /// <param name="args"></param>
        public void Send_S1F13_EstablishCommunicationsRequest(XfwArgs args)
        {
            try
            {

                var eqpID = args.Header[CItems.EqpID]?.ToString();
                var context = args.SECSData.Context;
                if (string.IsNullOrEmpty(eqpID))
                {
                    eqpID = args.SECSData.Context.Name;
                    context = XfwContext.GetSECSContext(eqpID);
                }

                var trans = context.GetTransaction(1, 13);
                var requestMsg = trans.Primary;
                var reply = context.SendAsync(trans);
                var replyMsg = reply.Result;
                if (replyMsg == null)
                {
                    args.SetReturnCodeNG("Send S1F13, Timeout");
                    CimLogger.Warning($"{Name}.Send_S1F13_EstablishCommunicationsRequest()  Timeout");

                    return;
                }
                Dict.Current.RCMConnStatus = conn;
                args.SetReturnCodeOK();
                CimLogger.Information($"{Name}.Send_S1F13_EstablishCommunicationsRequest() Execute Success.");
            }
            catch (Exception ex)
            {
                args.SetReturnCodeNG("S1F13:" + ex.Message);
                CimLogger.Error(ex, $"{Name}.Send_S1F13_EstablishCommunicationsRequest() Execute Error.");
            }
        }

        /// <summary>
        /// 发送报警信息(prober/ocr)
        /// </summary>
        /// <param name="args"></param>
        public void Send_S5F1_AlarmReport(XfwArgs args)
        {
            try
            {
                var rcmName = Dict.Current.RCMName;
                if (string.IsNullOrEmpty(rcmName))
                {
                    CimLogger.Warning($"{Name}.Send_S5F1_AlarmReport() rcmName is null");
                    return;
                }
                var eqpID = args.Header[CItems.EqpID]?.ToString();
                var context = XfwContext.GetSECSContext(rcmName);

                byte[] alcd = args.Header[CItems.Alcd]?.ToBi();

                // var aX2 = Convert.ToString(Convert.ToInt32(alcd[0]), 2);

                var alarmCode = AlarmCode.Set;
                //if (aX2.Length == 8 && aX2.Substring(0, 1) == "1")
                Dictionary<string, string> messagess = null;
                if (alcd[0] == 1)
                {
                    //开启报警
                    alarmCode = AlarmCode.Set;
                    //  messagess = TranslateMsg.GetTranslation("SendS5F1SetAlarm");
                }
                else
                {
                    //消除报警
                    alarmCode = AlarmCode.Clear;
                    //  messagess = TranslateMsg.GetTranslation("SendS5F1ClearAlarm");
                }
                var alid = args.Header[CItems.AlarmID]?.ToString();
                var altx = args.Header[CItems.AlarmMessage]?.ToString();
                var trans = context.GetTransaction(5, 1);
                var requestMsg = trans.Primary;
                requestMsg.Root.Add(new SECSItem(eSECS_FORMAT.BINARY) { Value = alarmCode, Name = "ALCD" });
                requestMsg.Root.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = alid, Name = "ALID" });
                requestMsg.Root.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = altx, Name = "ALTX" });
                requestMsg.Root.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = eqpID, Name = "EAPID" });

                var reply = context.SendAsync(trans);
                var replyMsg = reply.Result;

                if (messagess != null && messagess.Count != 0)
                {
                    ClientService.ShowMessage(eqpID, MessageType.Info, messagess, MessageOrigin.RCM);
                }
                if (replyMsg == null)
                {
                    args.SetReturnCodeNG("Send S5F1:Alarm Report, Timeout");
                    CimLogger.Warning($"{Name}.Send_S5F1_AlarmReport()  Timeout");

                    return;
                }
                CimLogger.Information($"{Name}.Send_S5F1_AlarmReport() Execute Success.alarmCode:{alarmCode}");
                var alcode = alarmCode == AlarmCode.Set ? "上报报警" : "清除报警";
                dbServer.InsertMsgInfo($"发送S5F1 {alcode}给RCM", eqpID, Dict.Current.lotInfo.lotId);

            }
            catch (Exception ex)
            {
                args.SetReturnCodeNG("S5F1:" + ex.Message);
                CimLogger.Error(ex, $"{Name}.Send_S5F1_AlarmReport() Execute Error.");
                return;
            }
        }
        public void Send_S5F1_AlarmStatusReport(XfwArgs args)
        {
            try
            {
                var rcmName = Dict.Current.RCMName;
                int alarmid = Convert.ToInt32(Dict.Current.AlarmID);
                var eqpID = args.Header[CItems.EqpID]?.ToString();
                var ocrName = Dict.Current.OCRName;
                if (string.IsNullOrEmpty(rcmName))
                {
                    //有可能EAP 和OCR 先连接，这时EAP给RCM发送状态，RCM 没收到    导致 OCR设备状态 = EAP设备状态！= RCM设备状态
                    if (Dict.Current.OCRCurrentStatus.ContainsKey(ocrName))
                    {
                        Dict.Current.OCRCurrentStatus[ocrName] = "";
                    }
                    else
                    {
                        Dict.Current.OCRCurrentStatus.Add(ocrName, "");
                    }
                    CimLogger.Information($"{Name}.{MethodBase.GetCurrentMethod().Name} rcmName is null");
                    return;

                }
                var eqpStatus = "";
                // var ocrName = "";
                int alarmcode = 1;
                Dictionary<string, string> messagess = null;
                var context = XfwContext.GetSECSContext(rcmName);
                if (alarmid == 1)
                {
                    alarmcode = 3;
                    eqpStatus = EQPState.ALARM.ToString();
                    messagess = TranslateMsg.GetTranslation("SendS5F1SetAlarm");
                }
                else if (alarmid == 0)
                {
                    alarmcode = 2;
                    Dict.Current.OCRCurrentStatus[ocrName] = "999";///定义一个不存在的设备id
                    eqpStatus = EQPState.RUN.ToString();
                    messagess = TranslateMsg.GetTranslation("SendS5F1ClearAlarm");
                }
                else
                {
                    return;
                }

                var trans = context.GetTransaction(6, 11);
                var requestMsg = trans.Primary;

                trans.Primary.Root.Item(1).Value = 201;
                trans.Primary.Root.Item(2).Value = 201;

                var rptList = new SECSItem(eSECS_FORMAT.LIST, "Report", "Report");
                rptList.Add(new SECSItem(eSECS_FORMAT.U2) { Value = 201 });

                var svsList = new SECSItem(eSECS_FORMAT.LIST, "SVs", "Status Variable Values");
                svsList.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = eqpID });
                svsList.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = alarmcode });  //current process state  

                rptList.Add(svsList);
                trans.Primary.Root.Item(3).Add(rptList);
                if (messagess != null && messagess.Count != 0)
                {
                    ClientService.ShowMessage(eqpID, MessageType.Info, messagess, MessageOrigin.RCM);
                }
                var reply = context.SendAsync(trans);
                var replyMsg = reply.Result;
                if (replyMsg == null)
                {
                    args.SetReturnCodeNG(" Send_S6F11:EventReport_STS, Timeout");
                    CimLogger.Information($"{Name}.{MethodBase.GetCurrentMethod().Name} Timeout,  curState:{eqpStatus}");
                    return;
                }
                CimLogger.Information($"{Name}.{MethodBase.GetCurrentMethod().Name} Execute Success. curState:{eqpStatus}");
                dbServer.InsertMsgInfo($"发送S6F11 {eqpStatus}状态给RCM", eqpID, Dict.Current.lotInfo.lotId);
            }
            catch (Exception ex)
            {
                args.SetReturnCodeNG("S6F11_EventReport_STS:" + ex.Message);
                CimLogger.Error(ex, $"{Name}.Send_S6F11_EventReport_STS() Execute Error.");
                return;
            }

        }
        /// <summary>
        /// 上报设备状态(prober/ocr)
        /// 格式？
        /// </summary>
        /// <param name="args"></param>
        public void Send_S6F11_EventReport_STS(XfwArgs args)
        {
            try
            {
                var rcmName = Dict.Current.RCMName;
                var eqpID = args.Header[CItems.EqpID]?.ToString();
                if (string.IsNullOrEmpty(rcmName))
                {
                    //有可能EAP 和OCR 先连接，这时EAP给RCM发送状态，RCM 没收到    导致 OCR设备状态 = EAP设备状态！= RCM设备状态
                    if (Dict.Current.OCRCurrentStatus.ContainsKey(eqpID))
                    {
                        Dict.Current.OCRCurrentStatus[eqpID] = "";
                    }
                    else
                    {
                        Dict.Current.OCRCurrentStatus.Add(eqpID, "");
                    }
                    CimLogger.Information($"{Name}.{MethodBase.GetCurrentMethod().Name} rcmName is null");
                    return;

                }
                //  var PreState = args.Header[CItems.PreState]?.ToString();
                var curState = args.Header[CItems.CurState]?.ToString();
                if (string.IsNullOrEmpty(curState))
                {
                    CimLogger.Information($"{Name}.{MethodBase.GetCurrentMethod().Name} curState is null");
                    return;
                }
                //var IsChangeEQPStatus = Dict.Current.PreState == curState ? false : true;
                Dict.Current.PreState = curState;
                var context = XfwContext.GetSECSContext(rcmName);
                //Dictionary<string, string> messagess = null;
                string eqpStatus = "";
                switch (curState)
                {
                    case "1": 
                        // messagess = TranslateMsg.GetTranslation("SendS6F11EQPStatus", EQPState.IDLE.ToString());
                        eqpStatus = EQPState.IDLE.ToString();
                        break;
                    case "2":
                        //  messagess = TranslateMsg.GetTranslation("SendS6F11EQPStatus", EQPState.RUN.ToString());
                        eqpStatus = EQPState.RUN.ToString();

                        break;
                    case "3":
                        //messagess = TranslateMsg.GetTranslation("SendS6F11EQPStatus", EQPState.ALARM.ToString());
                        eqpStatus = EQPState.ALARM.ToString();
                        break;                     
                }
                var trans = context.GetTransaction(6, 11);
                var requestMsg = trans.Primary;

                trans.Primary.Root.Item(1).Value = 201;
                trans.Primary.Root.Item(2).Value = 201;

                var rptList = new SECSItem(eSECS_FORMAT.LIST, "Report", "Report");
                rptList.Add(new SECSItem(eSECS_FORMAT.U2) { Value = 201 });

                var svsList = new SECSItem(eSECS_FORMAT.LIST, "SVs", "Status Variable Values");
                svsList.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = eqpID });
                svsList.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = curState });  //current process state  

                // svsList.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = PreState });  //previous process state 
                rptList.Add(svsList);
                trans.Primary.Root.Item(3).Add(rptList);
                //if (messagess != null && messagess.Count != 0)
                //{
                //    ClientService.ShowMessage(eqpID, MessageType.Info, messagess, MessageOrigin.RCM);
                //}
                var reply = context.SendAsync(trans);
                var replyMsg = reply.Result;
                if (replyMsg == null)
                {
                    args.SetReturnCodeNG(" Send_S6F11:EventReport_STS, Timeout");
                    CimLogger.Information($"{Name}.{MethodBase.GetCurrentMethod().Name} Timeout,  curState:{eqpStatus}");
                    return;
                }
                CimLogger.Information($"{Name}.{MethodBase.GetCurrentMethod().Name} Execute Success. curState:{eqpStatus}");
                dbServer.InsertMsgInfo($"发送S6F11 {eqpStatus}状态给RCM", eqpID, Dict.Current.lotInfo.lotId);
            }
            catch (Exception ex)
            {
                args.SetReturnCodeNG("S6F11_EventReport_STS:" + ex.Message);
                CimLogger.Error(ex, $"{Name}.Send_S6F11_EventReport_STS() Execute Error.");
                return;
            }
        }
        /// <summary>
        ///上报lot信息
        ///格式？
        /// </summary>
        /// <param name="args"></param>
        public void Send_S6F11_EventReport_LOT(XfwArgs args)
        {
            try
            {
                var rcmName = Dict.Current.RCMName;
                if (string.IsNullOrEmpty(rcmName))
                {
                    CimLogger.Warning($"{Name}.Send_S6F11_EventReport_LOT() rcmName is null");
                    return;
                }
                var eqpID = args.Header[CItems.EqpID]?.ToString();//args.SECSData.Context.Name;
                if (string.IsNullOrEmpty(eqpID))
                {
                    CimLogger.Warning($"{Name}.Send_S6F11_EventReport_LOT() eqpID is null");
                    return;
                }
                var RecipeName = args.Header[CItems.ProberRecipe]?.ToString();
                if (eqpID.Substring(4, 1) == "T") //测试机
                {
                    var testerRecipe = args.Header[CItems.ProberRecipe]?.ToString();
                    RecipeName = string.IsNullOrEmpty(testerRecipe) ? Dict.Current.lotInfo.testerProgram : testerRecipe;
                }
                else if (eqpID.Substring(4, 1) == "P") //探针台
                {
                    RecipeName = string.IsNullOrEmpty(RecipeName) ? Dict.Current.lotInfo.testerProgram : RecipeName;
                }
                var LotID = args.Header[CItems.LotID]?.ToString();  // "aa1233";//"AA111100";//

                var context = XfwContext.GetSECSContext(rcmName);

                var trans = context.GetTransaction(6, 11);
                var requestMsg = trans.Primary;

                trans.Primary.Root.Item(1).Value = 202;
                trans.Primary.Root.Item(2).Value = 202;

                var rptList = new SECSItem(eSECS_FORMAT.LIST, "Report", "Report");
                rptList.Add(new SECSItem(eSECS_FORMAT.U2) { Value = 202 });

                var svsList = new SECSItem(eSECS_FORMAT.LIST, "SVs", "Status Variable Values");
                svsList.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = eqpID });
                svsList.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = LotID });
                svsList.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = RecipeName });
                rptList.Add(svsList);

                trans.Primary.Root.Item(3).Add(rptList);

                var reply = context.SendAsync(trans);
                var replyMsg = reply.Result;
                if (replyMsg == null)
                {
                    args.SetReturnCodeNG(" Send S6F11:EventReport_LOT, Timeout");
                    CimLogger.Information($"{Name}.Send_S6F11_EventReport_LOT()  Timeout. LotID：{LotID}");

                    return;
                }
                CimLogger.Information($"{Name}.Send_S6F11_EventReport_LOT() Execute Success. LotID：{LotID}");
                dbServer.InsertMsgInfo($"发送S6F11 lot信息 ：{LotID}， 给RCM", eqpID, Dict.Current.lotInfo.lotId);

            }
            catch (Exception ex)
            {
                args.SetReturnCodeNG("Send_S6F11_EventReport_LOT:" + ex.Message);
                CimLogger.Error(ex, $"{Name}.Send_S6F11_EventReport_LOT() Execute Error.");
                return;
            }
        }
        /// <summary>
        /// 上报当前设备剩余跑货时间
        /// </summary>
        /// <param name="args"></param>
        public void Send_S6F11_EventReport_RemainingTime(XfwArgs args)
        {
            try
            {
                var rcmName = Dict.Current.RCMName;
                if (string.IsNullOrEmpty(rcmName))
                {
                    CimLogger.Warning($"{Name}.Send_S6F11_EventReport_RemainingTime() rcmName is null");
                    return;
                }
                var eqpID = args.Header[CItems.EqpID]?.ToString();//args.SECSData.Context.Name;
                if (string.IsNullOrEmpty(eqpID))
                {
                    CimLogger.Warning($"{Name}.Send_S6F11_EventReport_RemainingTime() eqpID is null");
                    return;
                }               
                var RemainingTime = args.Header[CItems.RemainingTime]?.ToString();  
                var context = XfwContext.GetSECSContext(rcmName);
                var trans = context.GetTransaction(6, 11);
                var requestMsg = trans.Primary;
                trans.Primary.Root.Item(1).Value = 13;
                trans.Primary.Root.Item(2).Value = 13;                    
                //list下直接两个参数，根据RCM 需要的格式进行了调整
                var svsList = new SECSItem(eSECS_FORMAT.LIST, "SVs", "Status Variable Values");
                svsList.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = eqpID });
                svsList.Add(new SECSItem(eSECS_FORMAT.ASCII) { Value = RemainingTime });           
                trans.Primary.Root.Item(3).Add(svsList);
                var reply = context.SendAsync(trans);
                var replyMsg = reply.Result;
                if (replyMsg == null)
                {
                    args.SetReturnCodeNG(" Send S6F11:Send_S6F11_EventReport_RemainingTime, Timeout");
                    CimLogger.Information($"{Name}.Send_S6F11_EventReport_RemainingTime()  Timeout. eqpID：{eqpID}");

                    return;
                }
                CimLogger.Information($"{Name}.Send_S6F11_EventReport_RemainingTime() Execute Success. eqpID：{eqpID}");
                dbServer.InsertMsgInfo($"上报S6F11 设备：{eqpID}  剩余跑货时间：[{RemainingTime}] 分钟  给RCM", eqpID, Dict.Current.lotInfo.lotId);
            }
            catch (Exception ex)
            {
                args.SetReturnCodeNG("Send_S6F11_EventReport_RemainingTime:" + ex.Message);
                CimLogger.Error(ex, $"{Name}.Send_S6F11_EventReport_RemainingTime() Execute Error.");
                return;
            }
        }

        #endregion

        #region 设备被动接收 Response
        /// <summary>
        /// 发送设备是否可以通讯
        /// </summary>
        /// <param name="args"></param>
        public void Receive_S1F1_AreYouThere(XfwArgs args)
        {
            try
            {
                try
                {
                    var context = args.SECSData.Context;
                    var trans = args.SECSData.Trans;
                    var msg = trans.Primary;
                    var reply = trans.Secondary;
                    context.ReplyMessage(args.SECSData.Trans);
                    CimLogger.Information($"{Name}.Receive_S1F1_AreYouThere() Execute Success.");
                }
                catch (Exception ex)
                {
                    args.SetReturnCodeNG("S1F1:" + ex.Message);
                    CimLogger.Error(ex, $"{Name}.Receive_S1F1_AreYouThere() Execute Error.");
                    return;
                }
            }
            catch (Exception ex)
            {
                args.SetReturnCodeNG("S1F1:" + ex.Message);
                CimLogger.Error(ex, $"{Name}.Send_S1F1_AreYouThere() Execute Error.");
            }
        }
        /// <summary>
        /// 返回建立通信信息
        /// </summary>
        /// <param name="args"></param>
        public void Receive_S1F13_EstablishCommunicationsSend(XfwArgs args)
        {
            try
            {
                var eqpID = args.SECSData.Context.Name;
                var context = XfwContext.GetSECSContext(eqpID);
                var trans = args.SECSData.Trans;

                var reply = trans.Secondary;
                reply.Root.Item(1).Value = 0;
                reply.Root.Item(2).Add(new SECSItem(eSECS_FORMAT.ASCII) { Name = "MDLN", Value = "EAPServer" });
                reply.Root.Item(2).Add(new SECSItem(eSECS_FORMAT.ASCII) { Name = "SOFTREV", Value = "v1.0.0" });

                context.ReplyMessage(args.SECSData.Trans);
                args.SetReturnCodeOK();
                CimLogger.Information($"{Name}.Receive_S1F13_EstablishCommunicationsSend() Execute Success.");
            }
            catch (Exception ex)
            {
                args.SetReturnCodeNG("S1F13:" + ex.Message);
                CimLogger.Error(ex, $"{Name}.Receive_S1F13_EstablishCommunicationsSend() Execute Error.");
                return;
            }
        }

        /// <summary>
        /// 请求offline
        /// </summary>
        /// <param name="args"></param>
        public void Receive_S1F15_OfflineRequest(XfwArgs args)
        {
            try
            {
                var context = args.SECSData.Context;
                var trans = args.SECSData.Trans;
                var msg = trans.Primary;
                var reply = trans.Secondary;
                context.ReplyMessage(args.SECSData.Trans);
                var eqpID = args.SECSData.Context.Name;
                args.Header[CItems.EqpID] = eqpID;
                args.Header[CItems.EqpStatus] = disConn;
                CimLogger.Information($"{Name}.Receive_S1F15_OfflineRequest() Execute Success.");
                args.SetReturnCodeOK();
            }
            catch (Exception ex)
            {
                args.SetReturnCodeNG("S1F15:" + ex.Message);
                CimLogger.Error(ex, $"{Name}.Receive_S1F15_OfflineRequest() Execute Error.");
                return;
            }
        }
        /// <summary>
        /// 请求online
        /// </summary>
        /// <param name="args"></param>
        public void Receive_S1F17_OnlineRequest(XfwArgs args)
        {
            try
            {
                var context = args.SECSData.Context;
                var trans = args.SECSData.Trans;
                var msg = trans.Primary;
                var reply = trans.Secondary;
                context.ReplyMessage(args.SECSData.Trans);
                var eqpID = args.SECSData.Context.Name;
                args.Header[CItems.EqpID] = eqpID;
                args.Header[CItems.EqpStatus] = conn;
                CimLogger.Information($"{Name}.Receive_S1F17_OnlineRequest() Execute Success.");
                args.SetReturnCodeOK();
            }
            catch (Exception ex)
            {
                args.SetReturnCodeNG("S1F17:" + ex.Message);
                CimLogger.Error(ex, $"{Name}.Receive_S1F17_OnlineRequest() Execute Error.");
                return;
            }
        }
        #endregion

    }
}
